import 'package:flutter/material.dart';

class StudentDJ extends StatefulWidget {
  const StudentDJ({Key? key}) : super(key: key);

  @override
  _StudentDJState createState() => _StudentDJState();
}

class _StudentDJState extends State<StudentDJ> {
  var _username=TextEditingController();
  int? _sex;
  List _hobby=[
    {
      "checked":true,
      "title":'吃饭'
    },
    {
      "checked":false,
      "title":'睡觉'
    },
    {
      "checked":true,
      "title":'打豆豆'
    }
  ];

  List<Widget> _getHobby(){
    List<Widget> tempList=[];
    for(var i=0;i<_hobby.length;i++){
      tempList.add(
        Row(
          children: [
            Text(this._hobby[i]['title']+':'),
            Checkbox(
              value: this._hobby[i]['checked'],
              onChanged: (v) {
                print(v);
                setState(() {
                  this._hobby[i]['checked']=v;
                });
              },
            ),
          ],
        )
      );
    };
    return tempList;
  }

  void _sexChanged(value){
    setState(() {
      this._sex=value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('学员登记系统'),
      ),
      body: Column(
        children: [
          TextField(
            decoration: InputDecoration(
              hintText: '输入学员信息',
            ),
            controller: _username,
          ),
          Row(
            children: [
              Text('男'),
              Radio(value: 1, groupValue: this._sex, onChanged:this._sexChanged),//注意提取写法，不是调取方法，而是赋值给onChanged
              Text('女'),
              Radio(value: 2, groupValue: this._sex, onChanged: this._sexChanged),
            ],
          ),
          //爱好(可能有很多列，用Wrap)
          Column(
            children:this._getHobby()
          ),


          SizedBox(height: 20),
          Container(
            width: double.infinity,
            child: ElevatedButton(
              child: Text('提交'),
              onPressed: (){
                print('账号:${_username.text} 性别${this._sex} 爱好${this._hobby}');
              },
            ),
          ),
        ],
      ),
    );
  }
}
